*Program name:      eventtimes_v*.do
*Written by:        Diane Alexander
*Date written:      April 3, 2020
*Last modified by:  Ezra Karger
*Date modified:     June 7, 2020

clear all

*Set sub-directory paths--same for all users:

global unacast  "${machine}/data/clean/unacast"
global stay_at_home "${machine}/data/clean/stay_at_home"
global womply "${machine}/data/clean/womply"
global nyt_cases "${machine}/data/clean/nyt_cases"
global census "${machine}/data/clean/census"
global trump "${machine}/data/clean/trump"
global acs "${machine}/data/clean/acs_2018"
global usda "${machine}/data/clean/usda_ers"
global weather "${machine}/data/clean/weather"
global covid_tracker "${machine}/data/clean/covid_tracker"

global outdata "${machine}/data/clean/"

global logs "${machine}/logs"
global tables "${machine}/tables/event_time/statedayfe"
global figures "${machine}/figures/event_time/statedayfe"

capture log close
log using "${logs}/eventtimes_with_stateday_fes.log", text replace




*Read in event-time analysis data, constructed in 2.4_eventtimes_no_stateday_fes.do

use "${outdata}/countydata_foreventtime", clear


*********************************************
***Event times based on stay at home order***
*********************************************

cd "${figures}"


local split "inc_all inc_high inc_low split_trump split_dem split_early split_late split_trumpe split_trumpl split_deme split_deml"

foreach i of local split {

local outcomes "distance visit pe_r_food pe_r_other pe_r_all log_pe_food log_pe_other log_pe_all"

foreach o of local outcomes {

tempfile `o'_`i'_s

preserve


*Subset to relevant sample and define label for graph

keep if `i'==1

sort county_fips date
*drop counties with no stay at home order 
drop if ever_stayathome==0

**keep from march 1
drop if date<mdy(3,1,2020)

*define the window for the eventtime graphs
local window 11
display `window'
local total (2*`window'+1)

**event time 0 is the day before effective date of order
gen eventtime=date-effective_date  +1

replace eventtime = -11 if eventtime<-11
replace eventtime = 11 if eventtime>11
tab eventtime

**event time 10 is the effective date of order
**shift event time variables because stata doesn't like negative categories
replace eventtime=eventtime+`window'
tab eventtime

**omitted cat is -5
fvset base 6 eventtime 

gen beta=.
gen se=.
gen highci=.
gen lowci=.



reghdfe `o' i.eventtime deaths_daily_incr log_deaths cases_daily_incr log_cases [aweight=pop_estimate_2018], absorb(date county_fips days_since_first_case i.state_fips##i.date) cluster(state_fips date)



**pull the regression results for the figure
matrix b=e(b)
matrix V=e(V)

collapse (mean) baseline = `o' if eventtime==10 & e(sample)==1 [aweight=pop_estimate_2018]
local baseline = baseline[1]

**create a dataset from the regression results
**the way I coded the figures makes it a bit of a pain to plot multiple event times on the same graph,
**though I agree that we should  
clear
set obs 23
gen n=_n
gen baseline=.
gen beta=.
gen se=.
gen highci=.
gen lowci=.
forval x=1/23 {
replace beta=b[1,`x'] if n==(`x')
replace se=sqrt(V[`x',`x']) if n==(`x') 
replace highci=beta+(1.96*se) if n==(`x')
replace lowci= beta-(1.96*se) if n==(`x')
}
sort n
rename n date
replace date = date-12
keep if inrange(date,-10,10)
replace lowci=. if beta==0
replace highci=. if beta==0
replace baseline = `baseline'


rename beta b_`o'_`i'_s
rename high h_`o'_`i'_s
rename low l_`o'_`i'_s
rename baseline base_`o'_`i'_s
drop se

save ``o'_`i'_s', replace
restore
}
}


*******************************************
***Event times based on first case/death***
*******************************************


drop if date<=mdy(2,23,2020)
local outcomes "distance visit pe_r_food pe_r_other pe_r_all log_pe_food log_pe_other log_pe_all "

*drop counties with no stay at home order 
drop if ever_stayathome==0
local medvar "case death"

**creates a variable with the first case or death
foreach m of local medvar {
gen first`m'_t=.
sort county_fips
bysort county_fips: replace first`m'_t=1 if `m's[_n]!=0 & `m's[_n-1]==0
replace first`m'_t=date*first`m'_t
bysort county_fips: egen first`m'_tt=min(first`m'_t)
bysort county_fips: egen first`m'=mean(first`m'_tt)
format first`m' %td
}
**drop counties where we don't have a 10 day period of no cases in our data 
**if firstcases<21981
drop if firstcase<mdy(3,7,2020)
local split "inc_all "

foreach i of local split {


foreach m of local medvar {
foreach o of local outcomes {
preserve
keep if `i'==1


tempfile `o'_`i'_`m'

*define the window for the eventtime graphs
local window 11
display `window'
local total (2*`window'+1)

**event time 0 is the day before the first case/death
gen eventtime=date-first`m'  +1

replace eventtime = -11 if eventtime<-11
replace eventtime = 11 if eventtime>11
tab eventtime

**event time 10 is the first case/death
replace eventtime=eventtime+`window'
tab eventtime

**omitted cat is -5
fvset base 6 eventtime 

gen beta=.
gen se=.
gen highci=.
gen lowci=.


reghdfe `o' i.eventtime deaths_daily_incr log_deaths cases_daily_incr log_cases [aweight=pop_estimate_2018], absorb(date county_fips days_since_first_case i.state_fips##i.date) cluster(state_fips date)


matrix b=e(b)
matrix V=e(V)

collapse (mean) baseline = `o' if eventtime==10 & e(sample)==1 [aweight=pop_estimate_2018]
local baseline = baseline[1]

**create a dataset from the regression results
**the way I coded the figures makes it a bit of a pain to plot multiple event times on the same graph,
**though I agree that we should  
clear
set obs 23
gen n=_n
gen baseline=.
gen beta=.
gen se=.
gen highci=.
gen lowci=.
forval x=1/23 {
replace beta=b[1,`x'] if n==(`x')
replace se=sqrt(V[`x',`x']) if n==(`x') 
replace highci=beta+(1.96*se) if n==(`x')
replace lowci= beta-(1.96*se) if n==(`x')
}
sort n
rename n date
replace date = date-12
keep if inrange(date,-10,10)
replace lowci=. if beta==0
replace highci=. if beta==0
replace baseline = `baseline'

rename beta b_`o'_`i'_`m'
rename high h_`o'_`i'_`m'
rename low l_`o'_`i'_`m'
rename baseline base_`o'_`i'_`m'
drop se
save ``o'_`i'_`m'', replace
restore
}
}
}



***Figure: for each outcome, plot the splits separately for stay at home event
local distance_lab "% change from pre-pandemic levels   "
local visit_lab "% change from pre-pandemic levels   "
local pe_r_food_lab "Revenue per establishment ($)"
local pe_r_other_lab "Revenue per establishment ($)"
local pe_r_all_lab "Revenue per establishment ($)"
local log_pe_food_lab "Log revenue per establishment ($)"
local log_pe_other_lab "Log revenue per establishment ($)"
local log_pe_all_lab "Log revenue per establishment ($)"

local outcomes "distance visit pe_r_food pe_r_other pe_r_all log_pe_food log_pe_other log_pe_all"
foreach o of local outcomes { 

use ``o'_inc_all_s', clear

merge 1:1 date using ``o'_inc_high_s', nogen
merge 1:1 date using ``o'_inc_low_s', nogen
merge 1:1 date using ``o'_split_trump_s', nogen
merge 1:1 date using ``o'_split_dem_s', nogen
merge 1:1 date using ``o'_split_early_s', nogen
merge 1:1 date using ``o'_split_late_s', nogen

merge 1:1 date using ``o'_split_trumpe_s', nogen
merge 1:1 date using ``o'_split_trumpl_s', nogen
merge 1:1 date using ``o'_split_deme_s', nogen
merge 1:1 date using ``o'_split_deml_s', nogen

sort date

**makes confidence interval meet at zero for omitted category
**alternative: just have it not plotted at all at 0
replace h_`o'_inc_high_s =0 if date==-5
replace l_`o'_inc_high_s =0 if date==-5
replace h_`o'_inc_low_s =0 if date==-5
replace l_`o'_inc_low_s =0 if date==-5
replace h_`o'_split_trump_s =0 if date==-5
replace l_`o'_split_trump_s =0 if date==-5
replace h_`o'_split_dem_s =0 if date==-5
replace l_`o'_split_dem_s =0 if date==-5
replace h_`o'_split_early_s =0 if date==-5
replace l_`o'_split_early_s =0 if date==-5
replace h_`o'_split_late_s =0 if date==-5
replace l_`o'_split_late_s =0 if date==-5

replace h_`o'_split_trumpe_s =0 if date==-5
replace l_`o'_split_trumpe_s =0 if date==-5
replace h_`o'_split_deme_s =0 if date==-5
replace l_`o'_split_deme_s =0 if date==-5
replace h_`o'_split_trumpl_s =0 if date==-5
replace l_`o'_split_trumpl_s =0 if date==-5
replace h_`o'_split_deml_s =0 if date==-5
replace l_`o'_split_deml_s =0 if date==-5

graph twoway ///
 (scatter b_`o'_inc_high_s date , scale(*1.4) c(l)  msize(vsmall) color(gs7)  lpattern(shortdash) ) ///
 (rarea h_`o'_inc_high_s l_`o'_inc_high_s date ,  color(gs7%15) ///
  xline(0,lcolor(gs8)) yline(0,lcolor(gs8))  ytitle("``o'_lab'") ysc(titleg(0.2cm)) xtitle("Days relative to event") ///
  scheme(s1color) ti("", span linegap(0.01) size(medsmall))) ///
  (scatter b_`o'_inc_low_s date , c(l)  msize(vsmall) color(gs11)) ///
 (rarea h_`o'_inc_low_s l_`o'_inc_low_s date ,  color(gs11%15) ///
 legend(order(1  "High income" 3 "Low income") region(lcolor(none)) ))  
 graph export "${figures}/`o'_incsplit_sdfe.pdf", replace
 
graph twoway ///
 (scatter b_`o'_split_trump_s date , scale(*1.4) c(l)  msize(vsmall) color(gs7) lpattern(shortdash) ) ///
 (rarea h_`o'_split_trump_s l_`o'_split_trump_s date ,  color(gs7%15) xline(0,lcolor(gs8)) yline(0,lcolor(gs8)) ///
 ytitle("``o'_lab'") ysc(titleg(0.2cm)) xtitle("Days relative to event") scheme(s1color) ti("", span linegap(0.01) size(medsmall))) ///
  (scatter b_`o'_split_dem_s date , c(l)  msize(vsmall) color(gs11)) ///
 (rarea h_`o'_split_dem_s l_`o'_split_dem_s date ,  color(gs11%15) ///
 legend(order( 1 "Republican (2016)" 3 "Democrat (2016)") region(lcolor(none)) )) 
 graph export "${figures}/`o'_trumpsplit_sdfe.pdf", replace
 
graph twoway ///
 (scatter b_`o'_split_early_s date , scale(*1.4) c(l)  msize(vsmall) color(gs7) lpattern(shortdash) ) ///
 (rarea h_`o'_split_early_s l_`o'_split_early_s date ,  color(gs7%15) xline(0,lcolor(gs8)) yline(0,lcolor(gs8)) ///
 ytitle("``o'_lab'") ysc(titleg(0.2cm)) xtitle("Days relative to event") scheme(s1color) ti("", span linegap(0.01) size(medsmall))) ///
  (scatter b_`o'_split_late_s date , c(l)  msize(vsmall) color(gs11)) ///
 (rarea h_`o'_split_late_s l_`o'_split_late_s date ,  color(gs11%15) ///
 legend(order( 1 "Early" 3 "Late") region(lcolor(none)) )) 
 graph export "${figures}/`o'_timingsplit_sdfe.pdf", replace
 
 graph twoway ///
 (scatter b_`o'_split_trumpe_s date , scale(*1.4) c(l)  msize(vsmall) color(gs7) lpattern(shortdash) ) ///
 (rarea h_`o'_split_trumpe_s l_`o'_split_trumpe_s date ,  color(gs7%15) xline(0,lcolor(gs8)) yline(0,lcolor(gs8)) ///
 ytitle("``o'_lab'") ysc(titleg(0.2cm)) xtitle("Days relative to event") scheme(s1color) ti("", span linegap(0.01) size(medsmall))) ///
  (scatter b_`o'_split_trumpl_s date , c(l)  msize(vsmall) color(gs11)) ///
 (rarea h_`o'_split_trumpl_s l_`o'_split_trumpl_s date ,  color(gs11%15) ///
 legend(order( 1 "Republican: Early" 3 "Republican: Late") region(lcolor(none)) )) 
 graph export "${figures}/`o'_trumptimingsplit_sdfe.pdf", replace
 
 graph twoway ///
 (scatter b_`o'_split_deme_s date , scale(*1.4) c(l)  msize(vsmall) color(gs7) lpattern(shortdash) ) ///
 (rarea h_`o'_split_deme_s l_`o'_split_deme_s date ,  color(gs7%15) xline(0,lcolor(gs8)) yline(0,lcolor(gs8)) ///
 ytitle("``o'_lab'") ysc(titleg(0.2cm)) xtitle("Days relative to event") scheme(s1color) ti("", span linegap(0.01) size(medsmall))) ///
  (scatter b_`o'_split_deml_s date , c(l)  msize(vsmall) color(gs11)) ///
 (rarea h_`o'_split_deml_s l_`o'_split_deml_s date ,  color(gs11%15) ///
 legend(order( 1 "Democrat: Early" 3 "Democrat: Late") region(lcolor(none)) )) 
 graph export "${figures}/`o'_trumpsplit_sdfe.pdf", replace
 
  export delimited  using  "${tables}/`o'_estimates_splits_sdfe.csv", replace

 }


***Figure: for each outcome, plot the  different events on one figure (ignore split)
***Have to rescale events so they match
local outcomes "distance visit pe_r_food pe_r_other pe_r_all log_pe_food log_pe_other log_pe_all" 
*fr_i_all fr_i_food fr_i_other"
foreach o of local outcomes {
use ``o'_inc_all_s', clear
merge 1:1 date using ``o'_inc_all_case', nogen
merge 1:1 date using ``o'_inc_all_death', nogen

**makes confidence interval meet at zero for omitted category
**alternative: just have it not plotted at all at 0
replace h_`o'_inc_all_s =0 if date==-5
replace l_`o'_inc_all_s =0 if date==-5
replace h_`o'_inc_all_case =0 if date==-5
replace l_`o'_inc_all_case =0 if date==-5
replace h_`o'_inc_all_death =0 if date==-5
replace l_`o'_inc_all_death =0 if date==-5

graph twoway ///
 (scatter b_`o'_inc_all_s date , scale(*1.4) c(l)  msize(vsmall) color(gs7) lpattern(shortdash) ) ///
 (rarea h_`o'_inc_all_s l_`o'_inc_all_s date ,  color(gs7%15) xline(0,lcolor(gs8)) yline(0,lcolor(gs8))  ytitle("``o'_lab'") ysc(titleg(0.2cm))  xtitle("Days relative to event") scheme(s1color) ti("", span linegap(0.01) size(medsmall))) ///
  (scatter b_`o'_inc_all_death date , c(l)  msize(vsmall) color(gs11)) ///
 (rarea  h_`o'_inc_all_death l_`o'_inc_all_death date , c(l) clpat(tight_dot) lw(medthick) msize(vsmall) color(gs11%15) ///
   legend(order(1 "Event: stay-at-home" 3 "Event: 1st death") row(1) region(lcolor(none)) )) 
 graph export "${figures}/`o'_byevent_sdfe.pdf", replace
 
export  delimited  using "${tables}/`o'_estimates_byevent_sdfe.csv", replace
}


clear
log close


